//
//  EGExcel.swift
//  EGExcel
//
//  Created by youzy01 on 2021/4/15.
//

import UIKit

public class EGExcel: UIView {
    private lazy var mainStackView: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.alignment = .fill
        stackView.distribution = .fill
        stackView.spacing = 0
        return stackView
    }()

    /// 左边固定部分
    private lazy var leftStackView: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.alignment = .fill
        stackView.distribution = .fill
        stackView.spacing = 0
        return stackView
    }()

    /// 右边横向滑动视图
    private lazy var scrollView: UIScrollView = {
        let view = UIScrollView()
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.bounces = false
        return view
    }()

    /// 右边可滑动部分
    private lazy var rightStackView: UIStackView = {
        let stackView = UIStackView()
        stackView.axis = .horizontal
        stackView.alignment = .fill
        stackView.distribution = .fill
        stackView.spacing = 0
        return stackView
    }()

    private var contentSize: CGSize = .zero

    /// 垂直方向是否可滑动
    public var isScrollOfVertical: Bool = true

    public override var intrinsicContentSize: CGSize {
        return contentSize
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setup()
    }

    public func configure(_ item: EGExcelData, didSelected: ((_ header: TextConvertible, _ label: UILabel, _ row: Int) -> Void)?) {
        if !leftStackView.isHidden && !leftStackView.arrangedSubviews.isEmpty {
            let views = leftStackView.arrangedSubviews.compactMap { $0 as? EGExcelTable }
            zip(views, item.leftData).forEach { (table, item) in
                table.collectionView.isScrollEnabled = isScrollOfVertical
                table.configure(header: item.header, dataSource: item.values)
            }
        } else {
            let leftTables = item.leftData.map { EGExcelTable(header: $0.header, dataSource: $0.values) }
            leftTables.forEach { (table) in
                table.collectionView.isScrollEnabled = isScrollOfVertical
                table.didSelected = { (t, label, indexPath) in
                    didSelected?(t, label, indexPath)
                }
                leftStackView.addArrangedSubview(table)
            }
            leftStackView.isHidden = leftTables.isEmpty
        }

        if !scrollView.isHidden && !rightStackView.arrangedSubviews.isEmpty {
            let views = rightStackView.arrangedSubviews.compactMap { $0 as? EGExcelTable }
            zip(views, item.rightData).forEach { (table, item) in
                table.collectionView.isScrollEnabled = isScrollOfVertical
                table.configure(header: item.header, dataSource: item.values)
            }
        } else {
            let rightTables = item.rightData.map { EGExcelTable(header: $0.header, dataSource: $0.values) }
            rightTables.forEach { (table) in
                table.collectionView.isScrollEnabled = isScrollOfVertical
                table.didSelected = { (t, label, indexPath) in
                    didSelected?(t, label, indexPath)
                }
                rightStackView.addArrangedSubview(table)
            }
            scrollView.isHidden = rightTables.isEmpty
            if rightTables.isEmpty {
                leftStackView.distribution = .fillEqually
            }
        }

        let row = item.rows

        contentSize = CGSize(width: bounds.width, height: CGFloat(row * 40 + row - 2))
        invalidateIntrinsicContentSize()
    }
}

private extension EGExcel {
    func setup() {
        mainStackView.translatesAutoresizingMaskIntoConstraints = false
        addSubview(mainStackView)
        NSLayoutConstraint.activate([
            mainStackView.topAnchor.constraint(equalTo: topAnchor),
            mainStackView.bottomAnchor.constraint(equalTo: bottomAnchor),
            mainStackView.leftAnchor.constraint(equalTo: leftAnchor),
            mainStackView.rightAnchor.constraint(equalTo: rightAnchor),
        ])

        rightStackView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.addSubview(rightStackView)
        NSLayoutConstraint.activate([
            rightStackView.topAnchor.constraint(equalTo: scrollView.topAnchor),
            rightStackView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
            rightStackView.leftAnchor.constraint(equalTo: scrollView.leftAnchor),
            rightStackView.rightAnchor.constraint(equalTo: scrollView.rightAnchor),
            rightStackView.heightAnchor.constraint(equalTo: scrollView.heightAnchor)
        ])

        mainStackView.addArrangedSubview(leftStackView)
        mainStackView.addArrangedSubview(scrollView)
    }
}
